package com.tistory.commons.utils;

import java.util.HashMap;
import java.util.ArrayList;

public class ParamParser
{
    private static HashMap<String,String>   map;
    private static ArrayList<String>        keyLt;

    public ParamParser( String params )
    {
        map     = new HashMap<String,String>();
        keyLt   = new ArrayList<String>();

        String  pLt[] = params.split(",");

        for( int i=0; i<pLt.length; i++ )
        {
            if( !"".equals(pLt[i].trim()) && pLt[i].indexOf("=")>0  )
            {
                String kvLt[]   = pLt[i].split("=");

                if(kvLt.length>2)
                {
                    map.put(kvLt[0],pLt[i].substring(pLt[i].indexOf("=")+1));
                }
                else
                {
                    if(kvLt.length>1)
                    {
                        map.put(kvLt[0], kvLt[1]);
                    }
                    else
                    {
                        map.put(kvLt[0], "");
                    }
                }

                keyLt.add(kvLt[0]);
            }
        }
    }

    public ParamParser( String params, String gbun )
    {
        map     = new HashMap<String,String>();
        keyLt   = new ArrayList<String>();

        String  pLt[] = params.split(gbun);

        for( int i=0; i<pLt.length; i++ )
        {
            if( !"".equals(pLt[i].trim()) && pLt[i].indexOf("=")>0  )
            {
                String kvLt[]   = pLt[i].split("=");

                if(kvLt.length>1)
                {
                    map.put(kvLt[0], kvLt[1]);
                }
                else
                {
                    map.put(kvLt[0], "");
                }

                keyLt.add(kvLt[0]);
            }
        }
    }

    public ArrayList<String> getKeys()
    {
        if(keyLt==null)
            return new ArrayList<String>();
        else
            return keyLt;
    }

    public String getValue( String key )
    {
        String  rtn = "";

        try
        {
            rtn = map.get(key);
        }
        catch(Exception e)
        {
            rtn = "";
        }

        return rtn;
    }
}
